#ifndef __FILE_API_H__
#define __FILE_API_H__

#include "gbl_types.h"

/* ### Local Declarations
 * ################################# */

/* invalid filename characters */
#define FILE_SCWILDCARD '?'     /* Wildcard for single character */
#define FILE_MCWILDCARD '*'     /* Wildcard for multiple characters */
#define FILE_SPECIALAT '@'      /* Wildcard for multiple characters */
#define FILE_CSEPARATOR '\\'    /* Separator characters */
#define FILE_ALT_CSEPARATOR '/' /* Alt Separator characters */
#define FILE_EOS '\0'           /* Terminating character marks end of string */

#ifndef SEEK_SET
#define SEEK_SET (0)
#endif

#ifndef SEEK_CUR
#define SEEK_CUR (1)
#endif

#ifndef SEEK_END
#define SEEK_END (2)
#endif

#define FILE_NAME_SIZE 128 /* Maximum length of filename       */

/* ################################# */
typedef char FDI_TCHAR;
typedef WORD FILE_ID; /* identifier for the file stream */

typedef struct tag_file_info
{
  /* filename plus end of string character */
  FDI_TCHAR file_name[FILE_NAME_SIZE + 1];
  int time;   /* updated time stamp when modified */
  int date;   /* updated date stamp when modified */
  DWORD size; /* size of file data in bytes */
  WORD owner_id;
  WORD group_id;
  WORD permissions;
  FILE_ID data_id; /* FDI identifier for file data */

  /* the following fields are needed for power loss recovery */

  FILE_ID plr_id; /* used for power loss recovery */
  int plr_time;   /* used for power loss recovery */
  int plr_date;   /* used for power loss recovery */
  DWORD plr_size; /* used for power loss recovery */
} FILE_INFO;

typedef enum fatTYPE_eAttributes
{
  fatTYPE_NORMAL = 0,     /* Writable file */
  fatTYPE_RDONLY = 0x01,  /* Read Only file */
  fatTYPE_HIDDEN = 0x02,  /* Hidden from directory view */
  fatTYPE_SYSTEM = 0x04,  /* Owned by the operating system */
  fatTYPE_VOLUME = 0x08,  /* Volume label entry */
  fatTYPE_DIR = 0x10,     /* Sub-directory */
  fatTYPE_ARCHIVE = 0x20, /* Copy of the file exists on this or another medium */
  fatTYPE_LONG_NAME = fatTYPE_RDONLY | fatTYPE_HIDDEN | fatTYPE_SYSTEM | fatTYPE_VOLUME
} fatTYPE_eAttributes;

typedef struct
{
  fatTYPE_eAttributes st_mode; /* File or directory attributes */
  DWORD st_size;               /* File size in bytes */
  unsigned int st_time;        /* Timestamp of last modification */
} fatTYPE_stat, fatTYPE_stat;

/***********************************************************************
 *
 * Name:		FDI_fopen
 *
 * Description: used in an implementation-specified matter to open or create a file and
 *    associate it with a stream.
 *
 * Parameters:
 *  char      *filename_ptr     [IN] const character string for file name specifier.
 *  char			*mode             [IN] const character string for type specification;
 *
 * Returns:
 *	the stream index used for the opened file.
 *    If an error is detected, FDI_fopen returns 0.
 *
 * Notes:
 *
 ***********************************************************************/
FILE_ID FDI_fopen(const char *filename_ptr, const char *mode);

/***********************************************************************
 *
 * Name:		FDI_fwrite
 *
 * Description: write file.
 *
 * Parameters:
 *	void		*buffer_ptr   	[IN]  pointer to buffer to be written
 *	size_t		element_size 	[IN]  size of element referenced by buffer pointer
 *	size_t		count    		  [IN]  number of elements to be read
 *	FILE_ID 	stream		    [IN]  stream index for the file to be closed.
 *
 * Returns:
 *		number of elements succesfully write
 * Notes:
 *
 ***********************************************************************/
size_t FDI_fwrite(const void *buffer_ptr,size_t element_size,size_t count,FILE_ID stream);

/***********************************************************************
 *
 * Name:		FDI_fread
 *
 * Description: read file.
 *
 * Parameters:
 *	void		*buffer_ptr   	[OUT]	pointer to buffer to place data read
 *	size_t		element_size 	[IN] 		size of element referenced by buffer pointer
 *	size_t		count    		  [IN]		number of elements to be read
 *	FILE_ID 	stream		    [IN]		stream index for the file to be closed.
 *
 * Returns:
 *		number of elements succesfully read
 * Notes:
 *
 ***********************************************************************/
size_t FDI_fread(void *buffer_ptr, size_t element_size, size_t count, FILE_ID stream);

/***********************************************************************
 *
 * Name:		FDI_fclose
 *
 * Description: used to close a file stream.
 *
 * Parameters:
 *  FILE_ID     stream      [IN]  stream index for the file to be closed.
 *
 * Returns:
 *		0:  pass
 * 	 	-1: fail
 * Notes:
 *
 ***********************************************************************/
int FDI_fclose(FILE_ID stream);

/***********************************************************************
 *
 * Name:		FDI_fseek
 *
 * Description: sets the file position indicator of the file specified  by stream.
 *				The new position, measured in bytes from the beginning of the file
 *				is obtained by adding offset to the position specified by wherefrom.
 *
 * Parameters:
 *  FILE_ID   stream      [IN]  stream index for the file
 *	long			offset 	    [IN]  the offset to obtain to the position indicator (in bytes)
 *	int				wherefrom   [IN]  the position to add offset(SEEK_SET, SEEK_CUR, SEEK_END)
 *
 * Returns:
  *		0:  pass
 * 	 	-1: fail
 * Notes:
 *
 ***********************************************************************/
int FDI_fseek(FILE_ID stream, long offset, int wherefrom);

/*
 * USAGE:
 *    status = FDI_feof(stream);
 */
// int FDI_feof(FILE_ID);

/***********************************************************************
 *
 * Name:        FDI_findfirst
 *
 * Description: begins a search for files specified wildcards..
 *
 * Parameters:
 *  char        *filename_ptr      [IN] const character string for file name specifier.
 *  FILE_INFO		*fileinfo_ptr	     [OUT]  pointer to type FILE_INFO structure filled with located file information
 *
 * Returns:
 *  		0: found
 * 	 	-1: not found
 *
 * Notes:
 *
 ***********************************************************************/
int FDI_findfirst(const char *filename_ptr, FILE_INFO *fileinfo_ptr);

/***********************************************************************
 *
 * Name:		FDI_findnext
 *
 * Description: begins a search for files specified wildcards..
 *
 * Parameters:
 *	FILE_INFO		*fileinfo_ptr		 [OUT]	pointer to type FILE_INFO structure filled with  file information
 *
 * Returns:
 *			0: found
 *		-1: not found
 *
 * Notes:
 *
 ***********************************************************************/

int FDI_findnext( FILE_INFO *fileinfo_ptr);

/***********************************************************************
 *
 * Name:		FDI_remove
 *
 * Description:  remove the file which is compatible to the file name from the existing  files list
 *
 * Parameters:
 *  char         *filename_ptr      [IN]  const character string for file name specifier.
 *
 * Returns:
 *		0:  pass
 * 	 	-1: fail
 * Notes:
 *
 ***********************************************************************/
int FDI_remove(const char *filename_ptr);


/***********************************************************************
 *
 * Name:		FDI_rename
 *
 * Description:  The function rename a file in file system.  
 *
 * Parameters:
 *  char         *oldname_ptr      [IN]  old name.
 *  char         *newname_ptr      [IN]  new name.
 *
 * Returns:
 *		0:  pass
 * 	 	-1: fail
 * Notes:
 *
 ***********************************************************************/
int FDI_rename(const FDI_TCHAR *oldname_ptr, const FDI_TCHAR *newname_ptr);

/***********************************************************************
 *
 * Name:		FDI_Stat
 *
 * Description:  get the file_name info.  
 *
 * Parameters:
 *  char         *file_name   [IN]  file name.
 *  fatTYPE_stat *stat        [OUT]  file info.
 *
 * Returns:
 *		0:  sucess
 * 	 	-1: fail
 * Notes:
 *
 ***********************************************************************/
int FDI_Stat(const char *file_name, fatTYPE_stat *stat);

/*
 * function: FDI_Access - Check file is exist
 *
 */
int FDI_Access(const char *path, UINT32 mode);

/*
 * function: MkDir - Create DIR
 *
 * syntax: int FDI_MkDir(const char *);
 */
int FDI_MkDir(const char *);

/***********************************************************************
 *
 * Name:		FDI_GetFreeSpaceSize
 *
 * Description:  get the free space size of the file system.  
 *
 * Parameters:
 *  None.
 *
 * Returns:
 *		free space size of the file system.  
 * Notes:
 *
 ***********************************************************************/
unsigned int FDI_GetFreeSpaceSize(void);


/***********************************************************************
 *
 * Name:		FDI_GetUsedSpaceSize
 *
 * Description:  get the used space size of the file system.  
 *
 * Parameters:
 *  None.
 *
 * Returns:
 *		used space size of the file system.  
 * Notes:
 *
 ***********************************************************************/
unsigned int FDI_GetUsedSpaceSize(void);


/***********************************************************************
 *
 * Name:		FDI_GetUsedSpaceSize
 *
 * Description:  get the total space size of the file system.  
 *
 * Parameters:
 *  None.
 *
 * Returns:
 *		total space size of the file system.  
 * Notes:
 *
 ***********************************************************************/
unsigned int FDI_GetTotalSpaceSize(void);

#endif /* __FILE_API_H__ */
